@file:JvmName("Md5Utils")

package com.yl.androidutilscenter.utils

import android.text.TextUtils
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException


/**
 * @author
 * @description:MD5 加密数据,不可逆
 * @date :2024/3/14
 * @version 1.0.0
 */
object Md5Utils {
    //首先初始化一个MessageDigest对象，该对象通过update方法获取原始数据，
    //并调用digest方法完成哈希计算，然后把字节数组逐位转换为十六进制数，最后拼装加密字符串
    @JvmStatic
    fun encrypyMD5(raw: String): String {
        if (TextUtils.isEmpty(raw)) {
            return ""
        }
        LogUtils.i("进行签名的数据:${raw}")
        val md5: MessageDigest
        try {
            md5 = MessageDigest.getInstance("MD5")
            val bytes = md5.digest(raw.toByteArray())
            val result = java.lang.StringBuilder()
            for (b in bytes) {
                var temp = Integer.toHexString(  0xff and b.toInt())
                if (temp.length == 1) {
                    temp = "0${temp}"
                }
                result.append(temp)
            }
            return result.toString()
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
        }
        return ""
    }
}